package com.friends.utils.dingding;

import com.github.jaemon.dinger.DingerSender;
import com.github.jaemon.dinger.core.entity.DingerRequest;
import com.github.jaemon.dinger.core.entity.enums.MessageSubType;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 推送钉钉消息工具类
 * @author liang.liu
 * @date 2023/04/23
 */
@Slf4j
@Component
public class NotifyUtil {

    @Autowired
    private DingerSender dingerSender;

    public void send(String content) {
        try {
            dingerSender.send(MessageSubType.MARKDOWN, DingerRequest
                    .request(content, "xxx消息通知"));
        } catch (Exception e) {
            log.error("发送推送消息失败，消息内容为" + content, e);
        }
    }

    public void sendFormat(String content, Object...args) {
        try {
            FormattingTuple format = MessageFormatter.arrayFormat(content, args);
            dingerSender.send(MessageSubType.MARKDOWN, DingerRequest.request(format.getMessage(), "xxx消息通知"));
        } catch (Exception e) {
            log.error("发送推送消息失败，消息内容为" + content, e);
        }
    }

}
